﻿using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections;
using System.Text;

[CustomEditor(typeof(TrackableVideoBehaviour))]
public class TrackableVideoEditor : Editor 
{
    private bool isDirty = false;

	public void OnEnable()
	{
        TrackableVideoBehaviour myTrackable;

        myTrackable = (TrackableVideoBehaviour)target;
        if (PrefabUtility.GetPrefabType(myTrackable) == PrefabType.Prefab)
        {
            Debug.Log("This is Prefab");
            return;
        }

        if (SceneManager.Instance.InitDone == false)
        {
            SceneManager.Instance.Init();
        }
	}

	void OnGUI() 
    {
	}

	public override void OnInspectorGUI()
	{
        TrackableVideoBehaviour myTrackable;
        myTrackable = (TrackableVideoBehaviour)target;

        if (PrefabUtility.GetPrefabType(myTrackable) == PrefabType.Prefab)
        {
            Debug.Log("This is Prefab");
            return;
        }

        myTrackable.UpdateTrackableInfo();

        isDirty = false;

        int prevGroupIdx = myTrackable.SelectedGroupIndex;
        myTrackable.SelectedGroupIndex = EditorGUILayout.Popup("Group Name", myTrackable.SelectedGroupIndex, myTrackable.GroupNameList.ToArray());
        if (prevGroupIdx != myTrackable.SelectedGroupIndex)
        {
            isDirty = true;
        }

        int prevTrkIdx = myTrackable.SelectedTrackableIndex;
        myTrackable.SelectedTrackableIndex = EditorGUILayout.Popup("Trackable Name", myTrackable.SelectedTrackableIndex, 
            myTrackable.TrackableNameList.ToArray());
        if (prevTrkIdx != myTrackable.SelectedTrackableIndex)
        {
            isDirty = true;
        }

        string prevVideoName = myTrackable.VideoName;
        myTrackable.VideoName = EditorGUILayout.TextField("Video Name", myTrackable.VideoName);
        if (string.Equals(prevVideoName, myTrackable.VideoName) == false)
        {
            isDirty = true;
        }

        MaxstAR.ITrackableVideoBehaviour.VideoTypeEnum prevVideoType = myTrackable.VideoType;
        myTrackable.VideoType = (MaxstAR.ITrackableVideoBehaviour.VideoTypeEnum)EditorGUILayout.EnumPopup("Video Type", (myTrackable.VideoType));
        if (prevVideoType != myTrackable.VideoType)
        {
            isDirty = true;
        }

        bool prevAlpha = myTrackable.Transparent;
        myTrackable.Transparent = EditorGUILayout.Toggle("Transparent", myTrackable.Transparent);
        if (prevAlpha != myTrackable.Transparent)
        {
            isDirty = true;
        }

        if (myTrackable.Transparent == true)
        {
            MaxstAR.ITrackableVideoBehaviour.SplitTypeEnum prevSplitType = myTrackable.SplitType;
            myTrackable.SplitType = (MaxstAR.ITrackableVideoBehaviour.SplitTypeEnum)EditorGUILayout.EnumPopup("Split Type", (myTrackable.SplitType));
            if (prevSplitType != myTrackable.SplitType)
            {
                isDirty = true;
            }
        }

        if (GUI.changed)
        {
            if (myTrackable != null)
            {
                if (isDirty)
                {
                    myTrackable.SetGroup(myTrackable.GroupNameList[myTrackable.SelectedGroupIndex]);
                    myTrackable.SetTrackableName(myTrackable.TrackableNameList[myTrackable.SelectedTrackableIndex]);
                    myTrackable.RefreshProperty();
                    EditorUtility.SetDirty(myTrackable);
                }
            }

            SceneManager.Instance.SceneUpdated();
        }
	}    
}
